package com.csz.servlet;

import com.csz.actions.Action;
import com.csz.actions.ActionBiotipo;
import com.csz.actions.ActionCalcularPesos;
import com.csz.actions.ActionCaracteristica;
import com.csz.actions.ActionCategoria;
import com.csz.actions.ActionEncerrar;
import com.csz.actions.ActionGravarScore;
import com.csz.actions.ActionImagem;
import com.csz.actions.ActionLogin;
import com.csz.actions.ActionSubCategoria;
import com.csz.actions.ActionVerPais;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Leonardo
 */
public class ServletMain extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private HashMap<String, Action> actions = new HashMap<String, Action>();

    public ServletMain() {
        inicialization();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String vAcao = request.getParameter("action");
        Action action = actions.get(vAcao);
        action.setParams(request, response);
        action.execute();
    }

    private void inicialization() {
        actions.put("login", new ActionLogin());
        actions.put("categoria", new ActionCategoria());
        actions.put("subCategoria", new ActionSubCategoria());
        actions.put("caracteristica", new ActionCaracteristica());
        actions.put("gravarScore", new ActionGravarScore());
        actions.put("gravarBiotipo", new ActionBiotipo());
        actions.put("calcularPesos" , new ActionCalcularPesos() );
        actions.put("verPais", new ActionVerPais());
        actions.put("verImagem", new ActionImagem());
        actions.put("encerrar", new ActionEncerrar());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
